package me.ctidy.assignment04.activity.auth;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import me.ctidy.assignment04.R;

/**
 * LoginActivity
 *
 * @author ctidy
 */
public class LoginActivity extends AppCompatActivity {

    private String username, pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initUI();
    }

    private void initUI() {
        // I. 绑定控件
        EditText etUsername = findViewById(R.id.al_et_username);
        EditText etPassword = findViewById(R.id.al_et_pwd);
        CheckBox cbRememberMe = findViewById(R.id.al_cb_remember_me);
        TextView tvRegister = findViewById(R.id.al_tv_register);
        Button btnLogin = findViewById(R.id.al_btn_login);

        // II. 注册事件监听器，并与控件绑定
        tvRegister.setOnClickListener(v -> {});  // TODO 跳转至注册界面
        btnLogin.setOnClickListener(v -> {
            // A. 从 UI 获取数据
            username = etUsername.getText().toString().trim();
            pwd = etPassword.getText().toString().trim();

            // B. 数据校验
            if (TextUtils.isEmpty(username)) {
                Toast.makeText(this, R.string.al_toast_lack_username, Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(pwd)) {
                Toast.makeText(this, R.string.al_toast_lack_pwd, Toast.LENGTH_SHORT).show();
                return;
            }

            // C. 业务逻辑处理
            if ("admin".equals(username) && "123456".equals(pwd)) {
                startActivity(
                        new Intent()
                                .setAction("me.ctidy.android.intent.action.PORTAL")
                                .putExtra("username", username)
                );
                finish();
                return;
            }
            Toast.makeText(this, R.string.al_toast_mismatch, Toast.LENGTH_SHORT).show();
        });

    }



}
